package com.newly.driver.sdk.device.video.cloud.stream;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.newly.driver.sdk.device.video.constants.DeviceConstant;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.DescribeDeviceStreamsData;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.DescribeDeviceStreamsResponse;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Arrays;
import java.util.List;

/**
 * @author gjm guan
 * @date 2021/10/21 14:50
 * @desc 查询直播流响应参数
 */
@Data
public class QueryLiveStreamResp {
    /**
     * 直播url总数
     */
    @ApiModelProperty("直播url总数")
    private int totalUrl;

    /**
     * 可用直播数量
     */
    @ApiModelProperty("可用直播数量")
    private int usableUrl;

    /**
     * 可用时间
     */
    @ApiModelProperty("可用时间")
    private int expireTime;

    /**
     * 直播流地址
     */
    @ApiModelProperty("直播流地址")
    private List<Payload> payloads;

    public QueryLiveStreamResp() {
        this.expireTime = DeviceConstant.STREAM_LIVE_EXPIRE_TIME;
    }

    /**
     * qiniu
     */
    public QueryLiveStreamResp(JSONObject jsonObject) {
        this();

        JSONObject playUrls = (JSONObject) jsonObject.getObj("playUrls");
        Payload webrtc = new Payload("webrtc", playUrls.getStr("webrtc"));
        Payload hls = new Payload("hls", playUrls.getStr("hls"));
        Payload flv = new Payload("flv", playUrls.getStr("flv"));
        Payload rtmp = new Payload("rtmp", playUrls.getStr("rtmp"));

        this.payloads = Arrays.asList(flv, hls, rtmp, webrtc);

        for (Payload payload : this.payloads) {
            this.totalUrl++;
            if (!StrUtil.isBlank(payload.getUrl())) {
                this.usableUrl++;
            }
        }
    }

    // tencent
    public QueryLiveStreamResp(DescribeDeviceStreamsResponse resp) {
        this();
        // 响应数据
        DescribeDeviceStreamsData data = resp.getData();

        // 流地址数据
        Payload flv = new Payload("flv", data.getFlvAddr());
        Payload hls = new Payload("hls", data.getHlsAddr());
        Payload rtmp = new Payload("rtmp", data.getRtmpAddr());
        Payload rtsp = new Payload("rtsp", data.getRtspAddr());
        this.payloads = Arrays.asList(flv, hls, rtmp, rtsp);

        for (Payload payload : this.payloads) {
            this.totalUrl++;
            if (!StrUtil.isBlank(payload.getUrl())) {
                this.usableUrl++;
            }
        }
    }

    /**
     * 详情数据
     */
    @Data
    public static class Payload {
        /**
         * 协议名称
         */
        @ApiModelProperty("协议名称")
        private String protocolName;

        /**
         * 直播地址
         */
        @ApiModelProperty("直播地址")
        private String url;

        public Payload(String protocolName, String url) {
            this.protocolName = protocolName;
            this.url = url;
        }
    }
}
